package com.flash.dataU.test.controller;

import com.flash.dataU.test.entity.Project;
import com.flash.dataU.test.entity.ProjectType;
import com.flash.dataU.test.entity.Server;
import com.flash.dataU.test.entity.ServerType;
import com.flash.dataU.test.service.impl.ProjectService;
import com.flash.dataU.test.service.impl.ProjectTypeService;
import com.flash.dataU.test.service.impl.ServerService;
import com.flash.dataU.test.service.impl.ServerTypeService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * .
 *
 * @author sunym1993
 * @version 0.0.1-SNAPSHOT
 * @since 2017年12月06日 17时34分
 */

@Controller
@RequestMapping("server")
public class ServerController {

    @Autowired
    private ServerService serverService;

    @Autowired
    private ServerTypeService serverTypeService;

    @Autowired
    private ProjectService projectService;

    /**
     * this method is only for Controller test, make sure that a Controller you write visit successfully
     * visit "http://localhost:8080/testController" after running, you can get a message
     * @return
     */
    @RequestMapping("findAllList")
    public String findAllList(HttpServletRequest request){

        List<Server> servers = serverService.findAllList();

        setTypeAndProjectAndUsedMemory(servers);

        request.setAttribute("servers", servers);

        return "server";
    }

    /**
     * this method is only for Controller test, make sure that a Controller you write visit successfully
     * visit "http://localhost:8080/testController" after running, you can get a message
     * @return
     */
    @RequestMapping("findByType")
    public String findByType(int typeId, HttpServletRequest request){

        Server server = new Server();
        ServerType serverType = serverTypeService.get(typeId);
        server.setType(serverType);
        List<Server> servers = serverService.findList(server);

        setTypeAndProjectAndUsedMemory(servers);

        request.setAttribute("servers", servers);

        return "server";
    }

    /**
     * 填充server中的类型、项目和负载
     * @param servers
     */
    private void setTypeAndProjectAndUsedMemory(List<Server> servers) {
        for (Server server : servers) {
            int typeId = server.getType().getId();
            ServerType serverType = serverTypeService.get(typeId);
            server.setType(serverType);

            List<Project> projects = projectService.findByServerId(server.getId());
            server.setProjects(projects);

            double usedMemory = 0;
            for (Project project : projects) {
                usedMemory += calTakenMemory(project.getMemoryInit(), project.getMemoryMax());
            }
            server.setUsed(usedMemory);
        }
    }

    private double calTakenMemory(double init, double max) {
        return (init + max) / 2.0;
    }

}
